<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/29
 * Time: 15:56
 */
namespace app\api\controller;

use think\Controller;
use think\Request;

class App extends Controller
{
    public function _initialize(){
        parent::_initialize();
        //当前控制器,方法
        define('ACTION_NAME',$this->request->action());
        define('CONTROLLER_NAME',$this->request->controller());
        $action = request()->route('action');
        $param =  Request::instance()->except('sign,action');
        cache(config('cache'));
        //过滤不需要验证的方法
        if (!in_array($action, ['common.send_note','index.index','commodity.index','index.banner','index.goods','shop.shop_index','shop.shop_goods','login.register','login.login','login.code_login','goods.search','goods.hot_words','common.express_code','login.retrieve_password'])) {
            //获取token会话
            $token  = input('param.token','');
            $sign  = input('param.sign','');
            if(empty($token)) {
                echo json_encode(['code' => 400010, 'msg' => config('error_code.400010')]);
                exit;
            }elseif (empty($sign)){
                echo json_encode(['code' => 400011, 'msg' => config('error_code.400011')]);
                exit;
            }else {
                $data = cache($token);
                if (empty($data)) {
                    echo json_encode(['code' => 400012, 'msg' => config('error_code.400012')]);
                    exit;
                }
                $param =  Request::instance()->except('sign,action');
                //验证
                if (!authenticate($param, $sign)) {
                    $code = 400013;
                    echo json_encode(['code'=>$code,'msg'=>config('error_code.'.$code)]);
                    exit;
                }
            }
        }
    }

    public function index(){
        $params = request()->param();
        $action = request()->route('action');
        $m = $c = $a = '';
        if(strpos($action,'@') !== false){
            $m = substr($action,strpos($action,'@')+1);
            $action = str_replace('@'.$m,'',$action);
        }
        if(strpos($action,'.') !== false){
            list($c,$a) = explode('.',$action);
        }else{
            $c = $action;
            $a = 'index';
        }
        $c = ucfirst($c); //首字母大写
        if($m){
            $classNamespace = "app\\api\\common\\$m\\$c";
        }else{
            $classNamespace = "app\\api\\common\\$c";
        }
        if(!class_exists($classNamespace)){
            $return = array(
                'code'      => 400014,
                'msg'   => config('error_code.400014')
            );
            echo json_encode($return);
            exit;
        }
        $noParam = array('goods.hot_words','common.express_code','index.banner','commodity.index');//不需要参数的接口
        if(in_array($action,$noParam)){
            $param = array();
        }else{
            $param = $params['data'];
            if (!is_array($param)) {
                $param = json_decode($param, true);
                if ($param === null || $param === false) {
                    return json(['code' => 400017, 'msg' => config('error_code.400017'),'interface' => $action ]);
                }
            }
        }
        $member_ip = getIPaddress();  //用户ip地址
        $add_interface_request_log = add_interface_request_log($params, $action, $member_ip, $param); //请求日志
        if ($add_interface_request_log['code'] != 200) {
            return json($add_interface_request_log);
        }

        $class = new \ReflectionClass($classNamespace);
        $classMethod = $class->getMethod($a);
        $return = call_user_func(array( $class->name,$classMethod->name ), $param);
        $return['interface'] = $action;
        return json($return);
    }


}